package services

import (
	"demo/models"
	"demo/pkg/database"
	"demo/pkg/middleware"
	"demo/utils"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

// 获取用户信息

func getUserInfo(c *gin.Context) *models.Userinfo {
	user, exists := c.Get("user")
	if !exists {
		logrus.Println("user not exists")
		return nil
	}
	return user.(*models.Userinfo)
}

func GetUserInfo(c *gin.Context) {
	resp := models.GetDefaultResp()
	user := getUserInfo(c)
	// userIDStr := c.Param("int")
	// userID, err := strconv.Atoi(userIDStr)
	// if err != nil {
	// 	logrus.Errorln(err)
	// 	c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
	// 	return
	// }
	// user, _ := database.GetUserinfoByUserId(userID)
	resp.Data = user
	c.JSON(http.StatusOK, resp)
}

// 修改用户信息
func UpdateUserinfo(c *gin.Context) {
	user := getUserInfo(c)
	resp := models.GetDefaultResp()
	if err := c.BindJSON(user); err != nil {
		setApiError(c, http.StatusInternalServerError, resp, err)
		return
	}
	err := database.UpdateUserinfo(user)
	if err != nil {
		logrus.Error("failed to update userinfo", err)
		return
	}
	token, err := middleware.CreateToken(user)
	if err != nil {
		logrus.Errorln("Token签名失败:", err)
		return
	}
	utils.SetCookie(c, token)
	c.JSON(http.StatusOK, resp)
}

// 修改用户密码
func UpadatePassword(c *gin.Context) {
	resetPassword := new(models.ResetPassword)
	resp := models.GetDefaultResp()
	user := getUserInfo(c)
	if err := c.ShouldBindJSON(&resetPassword); err != nil {
		setApiError(c, http.StatusInternalServerError, resp, err)
		return
	}

	if ok := CheckCodes(user.Username, resetPassword.Code); !ok {
		c.String(http.StatusBadRequest, "验证失败")
		return
	}
	hashedPassword := utils.HashPassword(resetPassword.Password, user.Email)
	user.Password = hashedPassword
	err := database.UpdateUserinfo(user)
	if err != nil {
		logrus.Error("failed to update userinfo", err)
		return
	}
	c.JSON(http.StatusOK, resp)
}

// 通过用户名获取用户信息
func SearchUser(c *gin.Context) {
	searchRequest := new(models.SearchUserRequest)
	resp := models.GetDefaultResp()
	if err := c.ShouldBindJSON(searchRequest); err != nil {
		setApiError(c, http.StatusInternalServerError, resp, err)
		return
	}

	users, count, err := database.SearchUser(searchRequest)
	if err != nil {
		setApiError(c, http.StatusInternalServerError, resp, err)
		return
	}

	resp.Data = users
	resp.Count = count
	c.JSON(http.StatusOK, resp)
}

//通过用户id获取用户用电信息
func GetUserEleInfo(c *gin.Context){
	user := getUserInfo(c)
	resp := models.GetDefaultResp()
	ele,err := database.GetUserEleInfo(user.Id)
	if err != nil {
		logrus.Error("failed to update userinfo", err)
		return
	}
	resp.Data=ele
	c.JSON(http.StatusOK, resp)
}


//获取最新的用户报告
func GetLatestReport(c *gin.Context){
	user := getUserInfo(c)
	resp := models.GetDefaultResp()
	report,err := database.GetLatestReport(user.Id)
	if err != nil {
		logrus.Error("failed to update userinfo", err)
		return
	}
	resp.Data=report
	c.JSON(http.StatusOK, resp)
}

func GetAllUsersInfo(c *gin.Context) {
	resp := models.GetDefaultResp()
	// 调用数据库函数获取所有用户信息
	users, err := database.GetAllUserinfo()
	if err != nil {
		setApiError(c, http.StatusInternalServerError, resp, err)
		return
	}

	resp.Data = users
	c.JSON(http.StatusOK, resp)
}